FROM m.daocloud.io/docker.io/library/python:3.9-slim

WORKDIR /app

# 安装系统依赖
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    libgl1 \
    libglib2.0-0 \
    libsm6 \
    libxext6 \
    libxrender-dev \
    libgomp1 \
    libstdc++6 \
    poppler-utils \
    && rm -rf /var/lib/apt/lists/*

# 设置pip镜像源
RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple \
    && pip config set global.trusted-host pypi.tuna.tsinghua.edu.cn

# 复制依赖文件并安装
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# 复制所有Python文件
COPY main.py .
COPY paddleocrPdfDeal.py .

# 创建并设置模型目录的所有权
RUN mkdir -p /app/custom_models/det \
    && mkdir -p /app/custom_models/rec \
    && mkdir -p /app/custom_models/cls

# 复制自定义模型（如果存在）
COPY custom_models/det/ /app/custom_models/det/
COPY custom_models/rec/ /app/custom_models/rec/
COPY custom_models/cls/ /app/custom_models/cls/

# 创建前端目录并复制前端文件
RUN mkdir -p /app/front
COPY front/ /app/front/

# 创建非特权用户并设置权限
RUN useradd -m appuser && \
    chown -R appuser:appuser /app
USER appuser

EXPOSE 8000

# 禁用所有可能导致AVX512指令的优化(否则报错)
ENV FLAGS_use_mkldnn_conv_self_attn_fuse=false
ENV FLAGS_use_mkldnn=false
ENV FLAGS_use_mkldnn_conv=false
ENV FLAGS_use_mkldnn_matmul=false
ENV FLAGS_use_mkl=false
ENV FLAGS_use_avx=false
ENV FLAGS_use_avx2=false
ENV FLAGS_use_avx512=false
ENV FLAGS_use_fma=false
ENV FLAGS_use_f16c=false

CMD ["python", "main.py"]